<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_kelompok_penerimaan extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function get_data($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_kelompok';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_pt = isset($_POST['kd_pt']) ? strval($_POST['kd_pt']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.*, b.nama_psb');
			$this->db->from('tbl_sd_psb_kelompok a');
			$this->db->join('tbl_sd_psb b', 'b.id_psb=a.id_psb');
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			if($page != 0)
				$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function get_data_edit($id){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_sd_psb_kelompok');
		$this->db->where('id_kelompok', $id);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		
		return json_encode($response);
	}
	
	function insert($data){
		//query insert data		
		$this->db->flush_cache();
        $this->db->set('nama_kelompok', $data['nama_kelompok']);
        $this->db->set('id_psb', $data['id_psb']);
        $this->db->set('tgl_mulai', FormatDateToMysql($data['tgl_mulai']));
        $this->db->set('tgl_selesai', FormatDateToMysql($data['tgl_selesai']));
        $this->db->set('kuota', $data['kuota']);
        $this->db->set('keterangan', $data['keterangan']);
        $this->db->set('status', '1');
        
		$result = $this->db->insert('tbl_sd_psb_kelompok');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function update($data){
		
		$this->db->flush_cache();
        $this->db->set('nama_kelompok', $data['nama_kelompok']);
        $this->db->set('id_psb', $data['id_psb']);
        $this->db->set('tgl_mulai', FormatDateToMysql($data['tgl_mulai']));
        $this->db->set('tgl_selesai', FormatDateToMysql($data['tgl_selesai']));
        $this->db->set('kuota', $data['kuota']);
        $this->db->set('keterangan', $data['keterangan']);
        //$this->db->set('status', '1');
        $this->db->where('id_kelompok', $data['id_kelompok']);
		
		$result = $this->db->update('tbl_sd_psb_kelompok');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function delete($kode){
		$this->db->flush_cache();
		
		$this->db->where('id_kelompok', $kode);
		$result = $this->db->delete('tbl_sd_psb_kelompok');
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
}

?>